<template>
	<view class="club_confirm_order">
		<uv-navbar placeholder autoBack bgColor="#CA1C1D">
			<template v-slot:left>
				<view class="left_slot">
					<image src="@/static/images/back.png"></image>
				</view>
			</template>
			<template v-slot:center>
				<view class="nav_title">确认订单</view>
			</template>
		</uv-navbar>

		<scroll-view scroll-y class="scroll_view">
			<view class="goods_info">
				<view class="goods_box">
					<image :src="data_info.product.img" class="cover"></image>
					<view class="goods_view">
						<view class="goods_deta">
							<view class="name">{{data_info.product.name}}</view>
							<view class="synopsis">{{data_info.product.synopsis}}</view>
						</view>
						<view class="price">
							<view class="unit">￥</view>
							<view class="value">{{data_info.price}}</view>
						</view>
					</view>
				</view>
			</view>
			<view class="cell_view">
				<view class="item" @click="active=1">
					<view class="key">微信支付</view>
					<image src="@/static/images/checked.png" v-if="active==1"></image>
					<image src="@/static/images/checked_no.png" v-else></image>
				</view>
				<view class="item" @click="active=2">
					<view class="key">余额支付</view>
					<image src="@/static/images/checked.png" v-if="active==2"></image>
					<image src="@/static/images/checked_no.png" v-else></image>
				</view>
				<view class="item">
					<view class="key">消费券:{{ shopping }}</view>
					<view class="value">
						<uv-switch v-model="choose.use_shopping" size="20" activeColor="#CA1C1D"
							@change="change"></uv-switch>
					</view>
				</view>
			</view>
			<view class="cell_view">
				<view class="item">
					<view class="key">投资人代付:</view>
					<view class="value">
						<uv-switch v-model="choose.invest" size="20" activeColor="#CA1C1D"
							@change="invest_id = ''"></uv-switch>
					</view>
				</view>
				<view class="item" v-if="choose.invest">
					<view class="key">投资人ID:</view>
					<view class="value">
						<input type="text" placeholder="请输入投资人ID" v-model="invest_id" class="input">
					</view>
				</view>
			</view>
		</scroll-view>

		<view class="footer">
			<view class="left">
				<view class="key">合计：</view>
				<view class="pricer">
					<view class="unit">￥</view>
					<view class="value"> {{data_info.real_total}}</view>
				</view>
			</view>
			<view class="right" @click="submit">提交订单</view>
		</view>
	</view>
</template>

<script setup>
	import {
		underwriteInfo,
		underwritePlace,
		underwritePayment,
		underwritePaymentNew
	} from "@/request/api";

	import useWechat from "@/hooks/useWechat.js";
	import useBase from "@/hooks/useBase.js";
	const {
		wxPay
	} = useWechat();
	const {
		router
	} = useBase();
	import {
		onLoad
	} from "@dcloudio/uni-app";

	import {
		ref,
		reactive
	} from "vue";

	const choose = reactive({
		use_shopping: false,
		invest: false
	});
	const change = () => {
		let value = choose.use_shopping;
		if (value) {
			let val = Number(data_info.value.price) - Number(shopping.value);
			val = val <= 0 ? 0 : val;
			data_info.value.real_total = val.toFixed(2);
		} else {
			data_info.value.real_total = data_info.value.price;
		};
	};

	const data_info = ref({
		id: "",
		real_total: 0.00,
		price: 0.00,
		product: {
			name: "",
			synopsis: "",
			content: "",
		}
	});
	const shopping = ref(0);
	onLoad(async (options) => {
		let id = options.id;
		let {
			data,
			shopping: shopping_val
		} = await underwriteInfo({
			id
		});
		data_info.value = data;
		shopping.value = shopping_val;
		change();
	});

	const routerTab = () => {
		setTimeout(() => {
			uni.switchTab({
				url: '/pages/tabbar/my'
			})
		}, 1000);
	};

	const active = ref("1");
	const invest_id = ref("");
	const payment = async ({
		id,
		real_price
	}) => {
		if (Number(real_price) <= 0) {
			uni.$uv.toast("操作成功");
			routerTab();
			return false;
		};

		let is_invest = choose.invest ? 1 : 0;
		let res = await underwritePaymentNew({
			pay_mode: active.value,
			is_invest: is_invest,
			id
		});
		let pay_data = res.pay_data;
		if (active.value == 2) {
			uni.$uv.toast("操作成功");
			routerTab();
			return false;
		};
		wxPay(pay_data, () => {
			uni.$uv.toast("操作成功");
			routerTab();
		});
	};
	const submit = async () => {
		let type = choose.use_shopping ? 1 : 0;
		let {
			id,
			real_price
		} = await underwritePlace({
			id: data_info.value.id,
			use_shopping: type,
			invest_id: invest_id.value
		});
		if (choose.invest) {
			uni.$uv.toast("已成功提交给投资人,请通知投资人付款!");
			routerTab();
			return false;
		}
		payment({
			id,
			real_price
		});


		// let res;
		// if (data_info.value.id == 9) {
		// 	res = await underwritePaymentNew({
		// 		pay_mode: active.value,
		// 		id
		// 	});
		// } else {
		// 	res = await underwritePayment({
		// 		pay_mode: active.value,
		// 		id
		// 	});
		// }
	};
</script>

<style scoped lang="scss">
	.club_confirm_order {
		height: calc(100vh - var(--window-top) - var(--window-bottom));
		background-color: #f5f5f5;

		.left_slot {
			image {
				width: 43rpx;
				height: 36rpx;
			}
		}

		.nav_title {
			color: #F5F5F5;
			font-size: 36rpx;
		}

		.scroll_view {
			height: calc(100vh - var(--window-top) - var(--window-bottom) - #{$navBarHeight} - 100rpx);
			padding: 20rpx 30rpx;
			box-sizing: border-box;

			.goods_info {
				border-radius: 16rpx;

				padding: 30rpx 30rpx 0;
				box-sizing: border-box;
				background: #FFFFFF;
				overflow: hidden;

				.goods_box {
					padding-bottom: 40rpx;
					box-sizing: border-box;
					display: flex;
					justify-content: space-between;

					.cover {
						width: 200rpx;
						height: 200rpx;
						border-radius: 10rpx;
					}

					.goods_view {
						flex: 1;
						padding-left: 20rpx;
						overflow: hidden;
						display: flex;
						flex-direction: column;
						justify-content: space-between;

						.goods_deta {
							.name {
								line-height: 36rpx;
								color: #333333;
								font-size: 30rpx;
								font-weight: bold;
								overflow: hidden;
								text-overflow: ellipsis;
								display: -webkit-box;
								-webkit-line-clamp: 2;
								-webkit-box-orient: vertical;
							}

							.synopsis {
								color: #999999;
								font-size: 20rpx;
								padding-top: 18rpx;
								box-sizing: border-box;
							}
						}

						.price {
							display: flex;
							align-items: center;

							.unit {
								color: #CA1C1D;
								font-size: 32rpx;
								font-weight: bold;
							}

							.value {
								color: #CA1C1D;
								font-size: 50rpx;
								font-weight: bold;
							}
						}
					}
				}
			}

			.cell_view {
				border-radius: 20rpx;
				margin: 20rpx 0;
				background: #FFFFFF;
				overflow: hidden;

				.item {
					height: 88rpx;
					padding: 0 30rpx;
					box-sizing: border-box;
					display: flex;
					justify-content: space-between;
					align-items: center;

					.key {
						color: #999999;
						font-size: 30rpx;
					}

					.value {
						flex: 1;
						color: #333333;
						font-size: 30rpx;
						display: flex;
						justify-content: flex-end;

						.input {
							text-align: right;
							border-bottom: 1px solid #CCCCCC;
						}
					}

					image {
						width: 40rpx;
						height: 40rpx;
					}
				}
			}
		}

		.footer {
			width: 100%;
			height: 100rpx;
			padding: 0 30rpx;
			box-sizing: border-box;
			background: #FFFFFF;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.left {
				display: flex;
				align-items: center;

				.key {
					color: #999999;
					font-size: 22rpx;
				}

				.pricer {
					display: flex;
					align-items: center;

					.unit {
						color: #CA1C1D;
						font-size: 32rpx;
						font-weight: bold;
					}

					.value {
						color: #CA1C1D;
						font-size: 50rpx;
						font-weight: bold;
					}
				}
			}

			.right {
				width: 214rpx;
				height: 72rpx;
				line-height: 72rpx;
				text-align: center;
				color: #FFFFFF;
				font-size: 26rpx;
				border-radius: 10rpx;
				background: #CA1C1D;
			}
		}
	}
</style>